<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
	
	<div class="layui-form-item">
	    <label class="layui-form-label required">菜单名称</label>
	    <div class="layui-input-block">
	        <input type="text" name="mname" lay-verify="required" lay-reqtext="菜单名称不能为空"  value="" class="layui-input">
	        <tip>填写要添加的菜单名称</tip>
	    </div>
	</div>
	
		<div class="layui-form-item">
			<label class="layui-form-label required">图片路径</label>
	    <div class="layui-input-block">
			<!-- name要和实体类一致 -->
	        <input type="text" name="mimage" id="uImg2" value="" class="layui-input"> 
			<img id="uImg" src="" style="width: 50px;height: 50px;"/>
	    </div>
	</div> 
	
	<!-- 上传图片 -->
	<div class="layui-form-item">
	    <label class="layui-form-label required">路径</label>
	    <div class="layui-input-block">
			<button type="button" class="layui-btn" id="test1">
				<i class="layui-icon">&#xe67c;</i>上传图片
			</button>
	    </div>
	</div>

    

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../js/http.js"></script>
<script>
    layui.use(['form','upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
			upload = layui.upload;

		//上传图片
		var uploadInst = upload.render({
			elem: '#test1', //绑定元素
			url: $api+'/menu/uploadImg',
			data:{
				/*
					第一次上传,URL为空,则直接进行对应上传操作
					上传完成后,修改URL地址
					如果有第二次,URL不为空,删除对应的URL图片
					删除对应组 group1
					group1/M00/00/00/rBAv0F5i9fqACaESAAA9kuZ8t5M000.png
					用户重复上传,需要把之前的上传的图片删掉
				*/
			   url:function(){
				   return $("#uImg").attr("src").replace("http://47.114.97.182/","");
			   }
			}
			//上传接口
			,done: function(res){
				// 查看地址是：http://47.114.97.182/
				console.log(res)
				$("#uImg").attr("src","http://47.114.97.182/"+res.data)
				$("#uImg2").attr("value","http://47.114.97.182/"+res.data)
			}
			,error: function(){
				//请求异常回调
			}
		});
		
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("是否提交?", {
                title: '确定'
            }, function () {
            	//ajax
            	$.post($api+"/menu/add",data.field,
            		(res)=>{
            			// 关闭弹出层
            			layer.close(index);
            			
            			var iframeIndex = parent.layer.getFrameIndex(window.name);
            			parent.layer.close(iframeIndex);
            		}
            	)
            	
            });
            return false;
        });

    });
</script>
</body>
</html>